//
//  GBInterMaster_APPLogic_CRC16.m
//  NetworkTest
//
//  Created by xiaosi on 16/7/26.
//  Copyright © 2016年 xiaosi. All rights reserved.
//

#import "GBInterMaster_APPLogic_CRC16.h"

@implementation GBInterMaster_APPLogic_CRC16

+ (int)calcCrc16:(Byte*)data withLen:(int)len{
    return [GBInterMaster_APPLogic_CRC16 calcCrc16:data withOffset:0 withLen:len];
}

+ (int)calcCrc16:(Byte*)data withOffset:(int)offset withLen:(int)len{
    return [GBInterMaster_APPLogic_CRC16 calcCrc16:data withOffset:offset withLen:len withPreval:0x00];
}

+ (int)calcCrc16:(Byte*)data withOffset:(int)offset withLen:(int)len withPreval:(int)preval{
    int ucCRCHi = (preval & 0xff00) >> 8;
    int ucCRCLo = preval & 0x00ff;
    int iIndex;
    for (int i = 0; i < len; ++i) {
        iIndex = (ucCRCLo ^ data[offset + i]) & 0x00ff;
        ucCRCLo = ucCRCHi ^ crc16_tab_h[iIndex];
        ucCRCHi = crc16_tab_l[iIndex];
    }
    return (((ucCRCHi & 0x00ff) << 8) | (ucCRCLo & 0x00ff)) & 0xffff;
}

@end
